package com.lzs.Controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzs.PO.Area;
import com.lzs.PO.Page;
import com.lzs.PO.Policy;
import com.lzs.Service.PolicyService;
import com.lzs.util.Msg;

@Controller
@RequestMapping(value = "/policy")
public class PolicyController {
	@Autowired
	PolicyService policyService;

	@RequestMapping(value = "/policy_list")
	public String policyList(Model model, HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize, String keyword) throws Exception {

		Page<Policy> page = this.policyService.findPolicyPageList(keyword, pageSize, pageNum);

		model.addAttribute("page", page);

		return "/policy/policy_list";

	}
	@RequestMapping(value = "/policy_add")
	public String addPolicy() {
		
		return"/policy/policy_add";
	}
	@ResponseBody
	@RequestMapping(value = "/policy_add_action")
	 public Msg addPolicyAction(String year,double line,double proportion) {
		
		try {
			
			Map<String,Object>map = new HashMap<String,Object>();
			
			map.put("year",year);
			
			map.put("line",line);
			
			map.put("proportion",proportion);
			
			this.policyService.addPolicy(map);
			
			return Msg.success();
		}catch(Exception e) {
			
			e.printStackTrace();
			
			return Msg.fail();
			
		}
	}
@RequestMapping(value = "/policy_update")
public String updatePolicy(Model model,String year) {
	
	Policy policy = this.policyService.findPolicyByYear(year);
	
	model.addAttribute("policy", policy);
	
	return"/policy/policy_update";
}
@ResponseBody
@RequestMapping(value = "/policy_update_action")
public Msg updatePolicyAction(String year,double line, double proportion) {
	   
	   try {
		   Map<String,Object>map = new HashMap<String,Object>();
		   
		   map.put("year", year);
		   
		   map.put("line", line);
		   
		   map.put("proportion", proportion);
		   
		   this.policyService.updatePolicy(map);
		   
		   return Msg.success();
		   
	   }catch(Exception e) {
		   
		   e.printStackTrace();
		   
		   return Msg.fail();
		   
	   }
	 
}
@ResponseBody
@RequestMapping(value = "policy_delete")
 public Msg deletePolicy(String year) {
	
	 try {
		 this.policyService.deletePolicy(year);
		 
		 return Msg.success();
		 
	 }catch(Exception e) {
		 
		 e.printStackTrace();
		 
		 return Msg.fail();
		 
	 }
	
}
}
